package lineareListen;

import java.util.NoSuchElementException;

public class Stack
{
    private ListElement stackpointer;

    boolean isEmpty()
    {
        return stackpointer == null;
    }

    void push(Object newObject)
    {
        ListElement newElement = new ListElement(newObject, stackpointer);
        stackpointer = newElement;
    }

    public ListElement getStackpointer()
    {
        return stackpointer;
    }

    public void setStackpointer(ListElement stackpointer)
    {
        this.stackpointer = stackpointer;
    }

    Object pop()
    {
        if (isEmpty())
        {
            throw new NoSuchElementException("pop() for empty stack");
        }
        Object popObject = stackpointer.getDaten();
        stackpointer = stackpointer.getNaechstes();
        return popObject;
    }

    void durchlaufe()
    {
        if (!(isEmpty()))
        {
            ListElement current = stackpointer;
            while (current != null)
            {
                System.out.println(current.getDaten() + " ");
                current = current.getNaechstes();
            }
        }
        else
        {
            System.out.println("Liste ist leer!");
        }
    }
}
